package com.fyt.service.Impl.pasture;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fyt.controller.utils.Code;
import com.fyt.controller.utils.Result;
import com.fyt.dto.pasture.AddPastureDTO;
import com.fyt.dto.pasture.UpdatePastureDTO;
import com.fyt.entity.Animalvariety;
import com.fyt.entity.Pasture;
import com.fyt.mapper.AnimalvarietyMapper;
import com.fyt.mapper.PastureMapper;
import com.fyt.service.Impl.dictionary.specific.AnimalvarietyServiceImpl;
import com.fyt.service.dictionary.specific.AnimalvarietyService;
import com.fyt.service.pasture.CityService;
import com.fyt.service.pasture.PastureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gdy
 * @since 2023-12-20
 */
@Service
public class PastureServiceImpl extends ServiceImpl<PastureMapper, Pasture> implements PastureService {
    @Autowired
    private PastureMapper pastureMapper;
    @Autowired
    private AnimalvarietyService animalvarietyService;

    /**
     * 查询牧场列表（包含模糊查询）
     * @param map
     * @return
     */
    @Override
    public Result getPastureList(Map<String, String> map) {
//        获取前端需要查看的页数
        Integer page = map.get("pageNumber")==null?1:Integer.parseInt(map.get("pageNumber"));
        Page<Pasture> pasturePage = new Page<>(page,Code.page_size);
        QueryWrapper<Pasture> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(map.get("pastureName")!=null,Pasture::getPastureName,map.get("pastureName"))
                .or()
                .like(map.get("enable")!=null,Pasture::getEnable,map.get("enable"));
        List<Pasture> pastureList = pastureMapper.selectPage(pasturePage,queryWrapper).getRecords();
        System.out.println(pastureList);
        return new Result(pastureList,pastureList.isEmpty() ? Code.get_err:Code.get_ok,pastureList.isEmpty() ?"error":"success");
    }

    //获取没有没被禁用的牧场
    @Override
    public Result getIsenablePastureList(Map<String, String> map) {
        QueryWrapper<Pasture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable","1");
        List<Pasture> pastures = pastureMapper.selectList(queryWrapper);
        return new Result(pastures,Code.get_ok,null);
    }

    /**
     * 牧场新增
     * @param addPastureDto
     * @return
     */
    @Override
    public Result addPasture(AddPastureDTO addPastureDto) {
        String pastureName = addPastureDto.getPastureName();

        QueryWrapper<Pasture> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Pasture::getPastureName,pastureName).select(Pasture::getPastureName);
        Pasture pasture = pastureMapper.selectOne(queryWrapper);
        if(pasture != null){
            return new Result(null, Code.save_err, "牧场名称已经存在！");
        }
        Pasture pasture1 = new Pasture();
        Animalvariety animalvariety = new Animalvariety();
        pasture1.setPastureName(pastureName);
        pasture1.setVenueSize(addPastureDto.getVenueSize());
        pasture1.setFarmSize(addPastureDto.getFarmSize());
//        省市区
        pasture1.setProvince(addPastureDto.getProvince());
        pasture1.setCity(addPastureDto.getCity());
        pasture1.setDistrict(addPastureDto.getDistrict());
//        详细地址
        pasture1.setAddress(addPastureDto.getAddress());
//        经纬度
        pasture1.setLongitude(addPastureDto.getLongitude());
        pasture1.setLatitude(addPastureDto.getLatitude());
//        填充字典值编号,通过前端传过来的畜牧名称进行查询对应id并填充到数据库字段中
        Map<String, String> map1 = new HashMap<>();
        map1.put("dictionary","1");
        map1.put("enable","all");
        Result list = animalvarietyService.getList(map1);
        String name = addPastureDto.getName();
        System.out.println(name);
        pasture1.setId(animalvarietyService.getIdByName(name));
//        默认存栏量为0,状态为启用0
        pasture1.setInventory(0);
        pasture1.setEnable(0);
        int count = pastureMapper.insert(pasture1);
        return new Result(null,count>0?Code.save_ok:Code.save_err,count>0?"success":"error");
    }

    /**
     * 修改牧场
     * @param
     * @return
     */
    @Override
    public Result updatePasture(UpdatePastureDTO updatePastureDTO) {
        Pasture updatePasture = new Pasture();
        updatePasture.setPastureName(updatePastureDTO.getPastureName());
        updatePasture.setVenueSize(updatePastureDTO.getVenueSize());
        updatePasture.setFarmSize(updatePastureDTO.getFarmSize());
        updatePasture.setProvince(updatePastureDTO.getProvince());
        updatePasture.setCity(updatePastureDTO.getCity());
        updatePasture.setDistrict(updatePastureDTO.getDistrict());
        updatePasture.setAddress(updatePastureDTO.getAddress());
        updatePasture.setLongitude(updatePastureDTO.getLongitude());
        updatePasture.setLatitude(updatePastureDTO.getLatitude());
//        畜牧种类修改
        String name = updatePastureDTO.getName();
        updatePasture.setId(animalvarietyService.getIdByName(name));

        updatePasture.setEnable(updatePastureDTO.getEnable());
        updatePasture.setPId(updatePastureDTO.getPId());
//        修改
        int count = pastureMapper.updateById(updatePasture);
        return new Result(null,count>0?Code.update_ok:Code.update_err,count>0?"success":"error");
    }

}
